package com.selune.online.ordering.system.project.common;

import com.selune.online.ordering.system.common.util.MultipartFileToFileUtils;
import com.selune.online.ordering.system.common.util.QiniuUploadUtils;
import com.selune.online.ordering.system.project.api.base.response.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import java.io.File;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * Created with IntelliJ IDEA.
 *
 * @author xiaoyp
 * @date 2020/4/1
 * <p>
 * Description:
 */
@RestController
@RequestMapping("/api/v1/ordering/common")
@Slf4j
@Api(value = "公共接口")
public class CommonController {
	
	@Autowired
	private QiniuUploadUtils qiniuUploadUtils;

	@ApiOperation(value = "上传图片", notes = "上传图片")
	@ApiImplicitParam(name = "file", value = "文件", required = true, dataType = "File", paramType = "body")
	@PostMapping(value = "/upload/img", produces = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public Response<String> uploadImg(@RequestParam("file") MultipartFile multipartFile) throws Exception {
		
		File file = MultipartFileToFileUtils.multipartFileToFile(multipartFile);
		String path = qiniuUploadUtils.uploadFile(file, null);
		MultipartFileToFileUtils.deleteTempFile(file);
		Response<String> response = Response.success(path);
		System.out.println(response);
		return response;
	}
}
